<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>sessionStorage</title>
  </head>
  <body>
    <button onclick="saveData()">点我保存一个数据</button>
    <button onclick="readData()">点我读取一个数据</button>
    <button onclick="removeData()">点我删除一个数据</button>
    <button onclick="removeAll()">点我清空所有数据</button>

    <script type="text/javascript">
      let p = { name: "张三", age: 18 };
      function saveData() {
        sessionStorage.setItem("msg", "hello!");
        sessionStorage.setItem("msg2", 666);
        sessionStorage.setItem("person", JSON.stringify(p));
      }
      function readData() {
        console.log(sessionStorage.getItem("msg2"));
        const person = sessionStorage.getItem("person");
        console.log(JSON.parse(person));
      }
      function removeData() {
        sessionStorage.removeItem("msg2");
      }
      function removeAll() {
        sessionStorage.clear();
      }
    </script>

    <!-- 1.666会自动转换成对应字符串 “666” -->
    <!-- 2.p是一个对象，要借助JSON.stringify函数将对象转变成字符串,JSON.parse将一个字符串解析成一个对象 -->
    <!-- 3.sessionStorage保存的数据在关闭浏览器之后不存在，准确来说是关闭该窗口就会清除数据，是一种缓存技术 -->
  </body>
</html>
